<%@ Page Trace="false" Language="C#" %>
<%@ Register TagPrefix="Wrox" TagName="PageHeader" Src="/ch7/global/pageheader.ascx"%>
<%@ Register TagPrefix="Wrox" TagName="PageFooter" Src="/ch7/global/pagefooter.ascx"%>
<%@ Register TagPrefix="Wrox" Namespace="Wrox" Assembly="viewsource" %>


<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>
<Wrox:PageHeader runat=server />

<H2>TextWriter</H2>
<script runat=server>

	MemoryStream _ms;
	
	protected void Page_Load( object Sender, EventArgs e )
	{
		_ms = new MemoryStream();
		TextWriter tw;
		
		tw = new StreamWriter( _ms );
		
		tw.WriteLine("Line 1");
		tw.WriteLine("Line 2");
		tw.Write("Line ");
		tw.WriteLine( 3 );
		
		tw.WriteLine( true );
		tw.WriteLine( false );
		
		// Important to call to ensure text writer writes out to stream
		
		tw.Flush();
		
		// Reset stream position ready for reading
				
		_ms.Seek( 0, SeekOrigin.Begin );
	}
	
	protected void WriteContentsToResponse( TextReader r )
	{
		string line;

		line = r.ReadLine();

		while ( line != null )
		{
			Response.Write(line);
			Response.Write( "<BR>" );
			line = r.ReadLine();
		}

	}

</script>

<%
	WriteContentsToResponse( new StreamReader(_ms) );
%>

<Wrox:PageFooter runat=server />
